package com.apes.auto.autoparts.service;

import com.alibaba.fastjson.JSONObject;
import com.apes.auto.autoparts.model.EpcPartNumberParse;
import com.apes.auto.autoparts.repository.EpcPartNumberParseListRepository;
import com.apes.auto.autoparts.repository.EpcPartNumberParseRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.MapUtil;
import com.apes.framework.util.ReflectUtil;
import com.apes.scm.masterdata.party.model.PartyRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service("epcPartNumberParseService")
public class EpcPartNumberParseService extends DomainService {

    @Autowired
    EpcPartNumberParseRepository epcPartNumberParseRepository;

    @Autowired
    EpcPartNumberParseListRepository epcPartNumberParseListRepository;



    /***
     * 功能：创建
     * @param request
     * @return
     */
    public EpcPartNumberParse create(SimpleRequest request) {
        PartyRole creator = null;

        String userId = request.get("userId");
        if(userId != null) {
            Object user = getUser(new Integer(userId));
            if( user != null ) creator = ReflectUtil.getFieldValue(user, "person");
        } else {
            creator = getPerson(request);
        }

        /***
         * 限制重复数据
         * 1. state == approve
         * 2. epcVersion
         * 3. partChineseName
         * 4. menuName
         */
        boolean isExist = epcPartNumberParseRepository.findOne(JpaDsl.toCriteria(
            "menuName", "EQ", request.get("menuName"),
            "epcVersion", "EQ", request.get("epcVersion"),
            "partChineseName", "EQ", request.get("partChineseName"),
            "state", "NE", "approve"
        )).orElse(null) != null;
        if(isExist) return null;

        EpcPartNumberParse epcPartNumberParse = request.getO(EpcPartNumberParse.class);
        epcPartNumberParse.setState("create");  //设置当前的状态
        epcPartNumberParse.setCreator(creator == null ? null : creator.getId());
        return epcPartNumberParseRepository.saveAndFlush(epcPartNumberParse);
    }

    /***
     * 功能： 修改
     * @param request
     * @return
     */
    public EpcPartNumberParse update(SimpleRequest request) {
        String id = request.get("id");
        long versions = (int) request.get("version"); // 获取当前请求的版本信息
        EpcPartNumberParse epcPartNumberParse = epcPartNumberParseRepository.findOne(id);
        if (epcPartNumberParse == null) throw new RuntimeException("配件解析单号【" + id + "】在系统中不存在。");
        if (epcPartNumberParse.getVersion() != versions) throw new RuntimeException("记录已更新或已删除，请重新提交！");  // 用于判断当前版本是否被修改
        EpcPartNumberParse requestData = request.getO(EpcPartNumberParse.class);
        requestData = epcPartNumberParseRepository.saveAndFlush(requestData);
        return requestData;
    }



    /***
     * 功能： 删除
     * @param request
     * @return
     * @throws RuntimeException
     */
    public String delete(SimpleRequest request) throws RuntimeException {
        String id = request.get("id");
        long versions = (int) request.get("version"); // 获取当前请求的版本信息
        EpcPartNumberParse epcPartNumberParse = epcPartNumberParseRepository.findOne(id);
        if (epcPartNumberParse == null) throw new RuntimeException("配件解析单号【" + id + "】在系统中不存在。");
        if (epcPartNumberParse.getVersion() != versions) throw new RuntimeException("记录已更新或已删除，请重新提交！");  // 用于判断当前版本是否被修改

        EpcPartNumberParse requestData = request.getO(EpcPartNumberParse.class);

        epcPartNumberParseRepository.delete(requestData.getId());

        return "删除成功！";
    }


    /***
     * 转审核
     */
    public EpcPartNumberParse transApprove(SimpleRequest request) {
        EpcPartNumberParse epcPartNumberParse = epcPartNumberParseRepository.findOne((String) request.get("id"));
        epcPartNumberParse.setState("transApprove");  //设置当前的状态为转审核
        epcPartNumberParse.setTransferApproveId(getPerson(request));
        epcPartNumberParse.setTransferApproveDate(new Date());
        return epcPartNumberParseRepository.saveAndFlush(epcPartNumberParse);
    }

    /***
     * 退回
     */
    public EpcPartNumberParse returnBack(SimpleRequest request) {
        EpcPartNumberParse epcPartNumberParse = epcPartNumberParseRepository.findOne((String) request.get("id"));
        epcPartNumberParse.setState("create");  //设置当前的状态为创建
        return epcPartNumberParseRepository.saveAndFlush(epcPartNumberParse);
    }


    /***
     * 功能 审核
     * @param request
     * @return
     * @throws RuntimeException
     */
    public JSONObject approve(SimpleRequest request) throws RuntimeException {

        //设置单据状态
        EpcPartNumberParse epcPartNumberParse = epcPartNumberParseRepository.findOne((String) request.get("id"));
        epcPartNumberParse.setState("approve");  //设置当前的状态为结单
        epcPartNumberParse.setApproveDate(new Date()); // 设置审核人  审核日期
        epcPartNumberParse.setApproveId(getPerson(request));
        return new JSONObject(MapUtil.mapped(epcPartNumberParseRepository.saveAndFlush(epcPartNumberParse)));
    }
}
